package tech.aistar.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.UserLogin;
import tech.aistar.entity.UserLoginCustomVo;
import tech.aistar.mapper.UserInfoMapper;
import tech.aistar.mapper.UserLoginMapper;
import tech.aistar.service.IUserLoginService;

import java.util.List;


@Service
@Transactional
public class UserLoginServiceImpl implements IUserLoginService {
    @Autowired
    private UserLoginMapper userLoginMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public UserLoginCustomVo findAllInfo(Integer id) {
        UserLoginCustomVo userLoginCustomVo = (UserLoginCustomVo) userLoginMapper.findById(id);
        userLoginCustomVo.setUserInfo(userInfoMapper.findById(id));
        return userLoginCustomVo;
    }

    @Override
    public UserLogin findByUserId(String userId) {
        UserLogin user = userLoginMapper.findByUserId(userId);
        return user;
    }

    @Override
    public void editUserLogin(String userId, String userPassword) {
        userLoginMapper.editUserLogin(userId, passwordEncoder.encode(userPassword));
    }

    @Override
    public void addCard(String name, String numder, String userId) {
        userLoginMapper.addCard(name,numder,userId);
    }

    @Override
    public void addUserLogin(UserLogin userLogin) {
        String pwd=userLogin.getUserPassword();
        userLogin.setUserPassword(passwordEncoder.encode(pwd));
        userLoginMapper.addUserLogin(userLogin);
    }

    @Override
    public List<String> findCards() {
        List<String> cards = userLoginMapper.findCards();
        return cards;
    }

    @Override
    public Integer findByCount() {
        Integer userCount = userLoginMapper.findByCount();
        return userCount;
    }
}
